﻿using System;
using System.Data.SqlClient;

namespace UFSSServerClasses
{
    public static class DBAccess
    {
        private const string connectionString = @"Data Source=198.7.228.15;Initial Catalog=ufss;User ID=matrob;Password=neit2010";

        public static string getConString()
        {
            return connectionString;
        }

        /// <summary>
        /// Gets UserID of the corresponding username
        /// </summary>
        /// <param name="username"></param>
        /// <returns>-1 on error</returns>
        public static int getUserID(string username)
        {
            int result = -1;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getuserid", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@username", username));
                    SqlDataReader r = c.ExecuteReader();
                    while (r.Read())
                    {
                        result = Int32.Parse(r.GetValue(0).ToString());
                    }
                    connection.Close();
                    return result;
                }
            }
            catch
            {
                return -1;
            }
        }

        public static string getUsername(int id)
        {
            string result = null;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getusername", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", id));
                    SqlDataReader r = c.ExecuteReader();
                    while (r.Read())
                    {
                        result = r.GetValue(0).ToString();
                    }
                    connection.Close();

                    return result;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
